package sma.guis;

import sma.agents.*;
import sma.infos.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class EmployeGui extends JFrame{
//	private String sNom;
//	private String sPreNom;
//	private boolean bSexuel; // true : male ; false : female
//	private String sDateNaissance;
//	private String sLieuNaissance;
//	private int iAge;
//	
//	// 1 : DEUG (bac+2)
//	// 2 : Licence (bac+3)
//	// 3 : Maitrise (bac+4)
//	// - 1er annee de master
//	// 4 : Master (bac+5)
//	// - Master professionnel
//	// - Master recherche
//	// - Master metiers de l'enseignement
//	// 5 : Doctorat (bac+8) 
//	private int iNiveauEtude;
//	
//	private String sEcoleLycenciePlusRecent;
//	
//	private int iAnneeLycencieePlusRecent;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Employe employe;
	private JTextField 	sNomField,
						sPreNomField,
						sDateNaissanceField,
						sLieuNaissanceField,
						iAgeField,
						sEcoleLycenciePlusRecentField,
						iAnneeLycencieePlusRecentField;
	private JRadioButton bSexuelMaleField,
						bSexuelFemaleField;
	private ButtonGroup bSexuelGroup;
	
	private JComboBox iNiveauEtudeField;
	
	public EmployeGui(Employe e){
		super(e.getLocalName());
		
		employe = e;
		
		JPanel p = new JPanel();
		
		p.setLayout(new GridLayout(9,2));
		
		p.add(new JLabel("Nom :"));
		this.sNomField = new JTextField(10);
		p.add(this.sNomField);
		
		p.add(new JLabel("Pre Nom :"));
		this.sPreNomField = new JTextField(10);
		p.add(this.sPreNomField);
		
//		p.add(new JLabel("Sexuel :"));
		this.bSexuelMaleField = new JRadioButton("Male");
		this.bSexuelFemaleField = new JRadioButton("Female");
		this.bSexuelGroup = new ButtonGroup();
		this.bSexuelGroup.add(this.bSexuelMaleField);
		this.bSexuelGroup.add(this.bSexuelFemaleField);
		this.bSexuelMaleField.setSelected(true);
		p.add(this.bSexuelMaleField);
		p.add(this.bSexuelFemaleField);
		
		p.add(new JLabel("Date de naissance :"));
		this.sDateNaissanceField = new JTextField(10);
		p.add(this.sDateNaissanceField);
		
		p.add(new JLabel("Lieu naissance :"));
		this.sLieuNaissanceField = new JTextField(10);
		p.add(this.sLieuNaissanceField);
		
		p.add(new JLabel("Age :"));
		this.iAgeField = new JTextField(2);
		p.add(this.iAgeField);
		
		p.add(new JLabel("Niveau Etude :"));
		this.iNiveauEtudeField = new JComboBox();
		this.iNiveauEtudeField.addItem("1 : DEUG (bac+2)");
		this.iNiveauEtudeField.addItem("2 : Licence (bac+3)");
		this.iNiveauEtudeField.addItem("3 : Maitrise (bac+4)");
		this.iNiveauEtudeField.addItem("4 : Master (bac+5)");
		this.iNiveauEtudeField.addItem("5 : Doctorat (bac+8)");
		p.add(this.iNiveauEtudeField);
		
		p.add(new JLabel("Ecole plus recent :"));
		this.sEcoleLycenciePlusRecentField = new JTextField(10);
		p.add(this.sEcoleLycenciePlusRecentField);
		
		p.add(new JLabel("Annee lycencie :"));
		this.iAnneeLycencieePlusRecentField = new JTextField(4);
		p.add(this.iAnneeLycencieePlusRecentField);
		
		JButton addButton = new JButton("Disposer Candidature");
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try{
					InfoCV info = new InfoCV();
					
					InfoPersonnelles infoPer = new InfoPersonnelles();
					
					infoPer.setNom(sNomField.getText().trim());
					infoPer.setPreNom(sPreNomField.getText().trim());
					infoPer.setDateNaissance(sDateNaissanceField.getText().trim());
					infoPer.setLieuNaissance(sLieuNaissanceField.getText().trim());
					infoPer.setAge(Integer.parseInt(iAgeField.getText().trim()));
					infoPer.setEcoleLycenciePlusRecent(sEcoleLycenciePlusRecentField.getText().trim());
					infoPer.setAnneeLycencieePlusRecent(Integer.parseInt(iAnneeLycencieePlusRecentField.getText().trim()));
					infoPer.setSexuel(false);
					if(bSexuelMaleField.isSelected())
						infoPer.setSexuel(true);
					
					infoPer.setNiveauEtude(iNiveauEtudeField.getSelectedIndex());
					
					info.setInfoPersonnelles(infoPer);
					
					//...
					System.out.println("avant insertion ...");
					
					employe.insererCandidature(info);
					
				}catch(Exception err){
					JOptionPane.showMessageDialog(EmployeGui.this, "Invalid values. " + err.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		p.add(addButton);
		
		getContentPane().add(p, BorderLayout.SOUTH);
		
		setResizable(false);
	}
	
	public void showGui(){
		this.pack();
		Dimension sizeScreen = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)sizeScreen.getWidth() / 2;
		int centerY = (int)sizeScreen.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.setVisible(true);
	}
}
